package cvi.com.reader.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cvi.com.reader.R;
import cvi.com.reader.activity.AuditionActivity;
import cvi.com.reader.activity.LoginActivity;
import cvi.com.reader.activity.PictureBookAuditionActivity;
import cvi.com.reader.activity.PictureWorksDetailsAcitivity;
import cvi.com.reader.activity.WorksDetailsAcitivity;
import cvi.com.reader.activity.WorksDetailsAcitivity2;
import cvi.com.reader.bean.ChartDataBean;
import cvi.com.reader.bean.DraftsBean;
import cvi.com.reader.bean.LoginBean;
import cvi.com.reader.tool.UtilsOnclickListen;

/**
 * Created by 梅涛 on 2020/7/24.
 */

public class PersonalCollectAdapter extends BaseAdapter {
    private Context context;
    private List<DraftsBean.DataBean> dataBeanList;
    private DeleteListener deleteListener;
    private List<ChartDataBean.DataBean> chartDataBeanList;

    public PersonalCollectAdapter(Context context, List<DraftsBean.DataBean> dataBeanList, List<ChartDataBean.DataBean> chartDataBeanList) {
        this.context = context;
        this.dataBeanList = dataBeanList;
        this.chartDataBeanList = chartDataBeanList;
    }

    public DeleteListener getDeleteListener() {
        return deleteListener;
    }

    public void setDeleteListener(DeleteListener deleteListener) {
        this.deleteListener = deleteListener;
    }

    public PersonalCollectAdapter(Context context, List<DraftsBean.DataBean> dataBeanList) {
        this.context = context;
        this.dataBeanList = dataBeanList;
    }

    @Override
    public int getCount() {
        return dataBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return dataBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viHolder = null;
        final DraftsBean.DataBean dataBean = dataBeanList.get(i);
        ChartDataBean.DataBean chartBean = chartDataBeanList.get(i);
        if (view == null) {
            view = LayoutInflater.from(context).inflate(
                    R.layout.item_grid_personal_collect, null);
            viHolder = new ViewHolder(view);

            view.setTag(viHolder);
        } else {
            viHolder = (ViewHolder) view.getTag();
        }
        viHolder.workName.setText(dataBean.getMaterial_name());
        viHolder.auther.setText(dataBean.getAuthor());
        viHolder.secondFloor.getBackground().setAlpha(178);
        viHolder.firstFloor.getBackground().setAlpha(178);
        viHolder.deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deleteListener.onDeleteClick(dataBean.getId());
            }
        });

        viHolder.readItNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UtilsOnclickListen.isFastClick()) {
                    Hawk.put("ChartDataBean", chartBean);
                    //  素材类型 0:文本类型 、1:绘本类型、2:影视类型
                    if ("0".equals(dataBean.getType())) {
                        Intent intent = new Intent(context, WorksDetailsAcitivity2.class);
                        context.startActivity(intent);
                    } else if ("1".equals(dataBean.getType())) {
                        Intent intent = new Intent(context, PictureWorksDetailsAcitivity.class);
                        context.startActivity(intent);
                    }
                }
            }
        });
        return view;
    }

    public interface DeleteListener {
        public void onDeleteClick(int id);
    }

    static class ViewHolder {
        @BindView(R.id.delete_btn)
        TextView deleteBtn;
        @BindView(R.id.work_name)
        TextView workName;
        @BindView(R.id.auther)
        TextView auther;
        @BindView(R.id.first_floor)
        LinearLayout firstFloor;
        @BindView(R.id.read_it_now)
        TextView readItNow;
        @BindView(R.id.second_floor)
        LinearLayout secondFloor;
        @BindView(R.id.rl_all)
        RelativeLayout rlAll;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
